export default {
  inject: ['refList', 'getFormConfig', 'globalOptionData', 'globalModel', 'getOptionData',
    'getGlobalDsv', 'getReadMode', 'getSubFormFieldFlag', 'getSubFormName', 'getDSResultCache',
    'getObjectFieldFlag', 'getObjectName'],
  methods: {
    hasConfig(configName) {
      if (!this.designer || !this.designer.selectedWidget) {
        return false
      }

      return this.designer.hasConfig(this.selectedWidget, configName)
    },

    emitDefaultValueChange() {
      if (!!this.designer) {
        if (!!this.designer.formWidget) {
          // let fieldWidget = this.designer.formWidget.getWidgetRef(this.designer.selectedWidget.options.name)
          let fieldWidget = this.designer.formWidget.getWidgetRefById(this.designer.selectedId,
            this.selectedWidget.options.name)
          if (!!fieldWidget && !!fieldWidget.refreshDefaultValue) {
            fieldWidget.refreshDefaultValue()
          }
        }
      }
    },

    emitFormModelChange(val) {
      if (!!this.designer) {
        if (!!this.designer.formWidget) {
          // let fieldWidget = this.designer.formWidget.getWidgetRef(this.designer.selectedWidget.options.name)
          let fieldWidget = this.designer.formWidget.getWidgetRefById(this.designer.selectedId,
            this.selectedWidget.options.name)
          if (!!fieldWidget && !!fieldWidget.syncUpdateFormModel) {
            fieldWidget.syncUpdateFormModel(val)
            fieldWidget.refreshDefaultValue()
          }
        }
      }
    },

    inputNumberHandler(value) {
      value = value.replace(/[^0-9]/gi, '')
    },

    onRemoteChange(val) {
      if (!!val) {
        this.optionModel.filterable = true
        //this.optionModel.allowCreate = false
      }
    },

    onAllowCreateChange(val) {
      if (!!val) {
        this.optionModel.filterable = true
      }
    },

    onMultipleSelected(val) {
      if (val) {
        // 清空已选项，否则console会报错！！
        let foundRef = this.designer.formWidget.getWidgetRefById(this.designer.selectedId,
          this.selectedWidget.options.name)
        if (!!foundRef && !!foundRef.clearSelectedOptions) {
          foundRef.clearSelectedOptions()
        }

        this.optionModel.defaultValue = []  //清空原默认值!!
      } else {
        if (!!this.optionModel.defaultValue && (this.optionModel.defaultValue.length > 0)) {
          this.optionModel.defaultValue = this.optionModel.defaultValue[0]
        } else {
          this.optionModel.defaultValue = ''
        }
      }

      //重新生成select组件
      const selectWidget = this.designer.formWidget.getSelectedWidgetRef()
      if (selectWidget && selectWidget.refreshWidgetKey) {
        selectWidget.refreshWidgetKey()
      }

      // 如果是人员选择组件并且和部门联动，则自动设置关联部门组件和人员选择组件的multiple属性相同
      if (this.selectedWidget.type === 'select-person' && this.optionModel.personnelDepartmentLinkage && this.optionModel.relatedDepartmentWidgets) {
        this.designer.formWidget.widgetRefList[this.optionModel.relatedDepartmentWidgets].field.options.multiple = val
      }
    },

  }
}
